package com.styletransfer.model;

/**
 * 分页请求类
 * 
 * 这个类用于封装分页查询的请求参数，包括页码、每页记录数等。
 * 前端在请求分页数据时，可以通过这个类来传递分页参数。
 */
public class PageRequest {
    
    private Integer pageNum;     // 页码，从1开始
    private Integer pageSize;    // 每页记录数
    private String orderBy;      // 排序字段
    private String orderDirection; // 排序方向：asc-升序，desc-降序
    
    // 构造函数
    public PageRequest() {
        this.pageNum = 1;
        this.pageSize = 10;
        this.orderDirection = "desc";
    }
    
    /**
     * 构造函数
     * 
     * @param pageNum 页码
     * @param pageSize 每页记录数
     */
    public PageRequest(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.orderDirection = "desc";
    }
    
    // Getters and Setters
    public Integer getPageNum() {
        return pageNum == null || pageNum < 1 ? 1 : pageNum;
    }
    
    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }
    
    public Integer getPageSize() {
        return pageSize == null || pageSize < 1 ? 10 : (pageSize > 100 ? 100 : pageSize);
    }
    
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    
    public String getOrderBy() {
        return orderBy;
    }
    
    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }
    
    public String getOrderDirection() {
        return "asc".equalsIgnoreCase(orderDirection) ? "asc" : "desc";
    }
    
    public void setOrderDirection(String orderDirection) {
        this.orderDirection = orderDirection;
    }
    
    /**
     * 获取开始行号
     * 
     * 用于MyBatis分页查询，计算从第几行开始查询
     * 
     * @return 开始行号
     */
    public Integer getOffset() {
        return (getPageNum() - 1) * getPageSize();
    }
    
    /**
     * 获取排序子句
     * 
     * 用于SQL查询中的ORDER BY子句
     * 
     * @return 排序子句
     */
    public String getOrderClause() {
        if (orderBy == null || orderBy.trim().isEmpty()) {
            return "";
        }
        return orderBy + " " + getOrderDirection();
    }
    
    @Override
    public String toString() {
        return "PageRequest{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", orderBy='" + orderBy + '\'' +
                ", orderDirection='" + orderDirection + '\'' +
                '}';
    }
}
